#include "processor.h"
#include "processor_context.h"
#include "uarch/sim.h"

using namespace std;

extern Simulator Sim;

Processor_context::Processor_context (processor *proc)
{
    this->my_processor = proc;
    this->thread = NULL;
    this->suspended = false;
    this->flush = false;
    this->peeked = false;
    this->outstanding_reqs = 0;
    outstanding_req_list.clear();
}

Processor_context::~Processor_context (void)
{
    if (thread)
        delete thread;

    while (outstanding_req_list.size ())
    {
        Preq *preq = outstanding_req_list.front ();
        outstanding_req_list.pop_front ();
        delete preq;
    }
}

Insn *Processor_context::fetch ()
{
    assert (outstanding_reqs < Sim.settings.mshrs_per_processor);
    assert (thread != NULL);

    return thread->fetch ();
}

Insn *Processor_context::peek ()
{
    assert (thread != NULL);
    return thread->peek ();
}


void Processor_context::dump_outstanding_requests ()
{
    int i = 0;
    LIST<Preq *>::reverse_iterator it;

    fprintf (stderr, "Outstanding Requests: %u\n", outstanding_reqs);
    for (it = outstanding_req_list.rbegin(); it != outstanding_req_list.rend(); it++)
    {
        fprintf (stderr, "Request %d - ", i++);
        (*it)->dump();
    }
}
